package com.cljz.makepassenger.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.widget.datetimpick.TimeSelector;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.kit.Kits;

/**
 * Created by changlianjiuzhou on 17/2/27.
 */

public class SelectTime extends LinearLayout implements View.OnClickListener {
    private TimeDataCallBack timeDataCallBack;
    protected TimeButton button1;
    protected TimeButton button2;
    protected TimeButton button3;
    protected TimeButton button4;
    protected TimeButton button5;
    protected TimeButton button6;
    protected TimeButton button7;
    private String msg = null;

    private List<TimeButton> buttonList = new ArrayList<>();

    public SelectTime(Context context) {
        super(context);
        init();

    }

    public SelectTime(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SelectTime(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void initdata(TimeDataCallBack timeDataCallBack, String startTime) {
        this.timeDataCallBack = timeDataCallBack;
        button1.setName(Kits.Date.getHm(System.currentTimeMillis()));

    }

    public void setchecknendable() {
        if (GetSystemTimeUtil.getWeekToDay().equals("6")) {
            if (GetSystemTimeUtil.noonOut() && !GetSystemTimeUtil.nightOut()) {
                button3.setClick(false, R.mipmap.todaynoon_r);
                button4.setClick(true, R.mipmap.tonight);
                button6.setClick(true, R.mipmap.frinight);
            } else if (GetSystemTimeUtil.noonOut() && GetSystemTimeUtil.nightOut()) {
                button4.setClick(false, R.mipmap.tonight_r);
                button3.setClick(false, R.mipmap.todaynoon_r);
                button6.setClick(false, R.mipmap.frinight_r);
            } else {
                button4.setClick(true, R.mipmap.tonight);
                button3.setClick(true, R.mipmap.todaynoon);
                button6.setClick(true, R.mipmap.frinight);
            }

        } else if (GetSystemTimeUtil.getWeekToDay().equals("7")) {
            button6.setClick(false, R.mipmap.frinight_r);

            if (GetSystemTimeUtil.noonOut() && !GetSystemTimeUtil.nightOut()) {
                button3.setClick(false, R.mipmap.todaynoon_r);
                button4.setClick(true, R.mipmap.tonight);
                button7.setClick(false, R.mipmap.stanight);
            } else if (GetSystemTimeUtil.noonOut() && GetSystemTimeUtil.nightOut()) {
                button4.setClick(false, R.mipmap.tonight_r);
                button3.setClick(false, R.mipmap.todaynoon_r);
                button7.setClick(false, R.mipmap.stanight_r);
            } else {
                button4.setClick(true, R.mipmap.tonight);
                button3.setClick(true, R.mipmap.todaynoon);
                button7.setClick(false, R.mipmap.stanight);
            }
        } else if (GetSystemTimeUtil.getWeekToDay().equals("1")) {
            button7.setClick(false, R.mipmap.stanight_r);
            button6.setClick(false, R.mipmap.frinight_r);
            if (GetSystemTimeUtil.noonOut() && !GetSystemTimeUtil.nightOut()) {
                button3.setClick(false, R.mipmap.todaynoon_r);
                button4.setClick(true, R.mipmap.tonight);
            } else if (GetSystemTimeUtil.noonOut() && GetSystemTimeUtil.nightOut()) {
                button4.setClick(false, R.mipmap.tonight_r);
                button3.setClick(false, R.mipmap.todaynoon_r);
            } else {
                button4.setClick(true, R.mipmap.tonight);
                button3.setClick(true, R.mipmap.todaynoon);
            }
        } else {
            if (GetSystemTimeUtil.noonOut() && !GetSystemTimeUtil.nightOut()) {
                button3.setClick(false, R.mipmap.todaynoon_r);
                button4.setClick(true, R.mipmap.tonight);
            } else if (GetSystemTimeUtil.noonOut() && GetSystemTimeUtil.nightOut()) {
                button4.setClick(false, R.mipmap.tonight_r);
                button3.setClick(false, R.mipmap.todaynoon_r);
            } else {
                button4.setClick(true, R.mipmap.tonight);
                button3.setClick(true, R.mipmap.todaynoon);
            }
        }

        invalidate();
    }

    private void init() {
        LinearLayout linearLayout = new LinearLayout(getContext());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.select_time_layout, null, false);
        initView(view);
        linearLayout.addView(view);
        linearLayout.setLayoutParams(layoutParams);
        addView(linearLayout);
    }

    private void initView(View rootView) {
        button1 = (TimeButton) rootView.findViewById(R.id.button1);
        button2 = (TimeButton) rootView.findViewById(R.id.button2);
        button3 = (TimeButton) rootView.findViewById(R.id.button3);
        button4 = (TimeButton) rootView.findViewById(R.id.button4);
        button5 = (TimeButton) rootView.findViewById(R.id.button5);
        button6 = (TimeButton) rootView.findViewById(R.id.button6);
        button7 = (TimeButton) rootView.findViewById(R.id.button7);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        buttonList.add(button1);
        buttonList.add(button2);
        buttonList.add(button3);
        buttonList.add(button4);
        buttonList.add(button5);
        buttonList.add(button6);
        buttonList.add(button7);

    }

    @Override
    public void onClick(View view) {
        int postion = -1;
        switch (view.getId()) {
            case R.id.button1:
                new TimeSelector(getContext(), new TimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        msg = time;
                        timeDataCallBack.getSelectTime(msg);
                    }
                }, GetSystemTimeUtil.getStartTimeandHHMM(), GetSystemTimeUtil.getEndTimeandHHMM()).show();
                //msg=button1.getText().toString();
                break;
            case R.id.button2:
                msg = GetSystemTimeUtil.getNowTwoLaterDate();
                timeDataCallBack.getSelectTime(msg);

                //msg=button2.getText().toString();
                break;
            case R.id.button3:
                // msg="you效果";

                setcheck(button3, R.mipmap.todaynoon, R.mipmap.todaynoon_r_r);
                msg = button3.isCheck ? GetSystemTimeUtil.getInDetailTime(GetSystemTimeUtil.getPresentSysNoontime()) : null;
                postion = button3.isCheck ? 3 : -1;
                timeDataCallBack.getSelectTime(msg);
                button1.setName(msg == null ? GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()) : GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailTime(GetSystemTimeUtil.getPresentSysNoontime())));
                clearCheck(button6, R.mipmap.frinight, R.mipmap.frinight_r);
                clearCheck(button5, R.mipmap.tomorrownight, R.mipmap.tomorrownight);
                clearCheck(button4, R.mipmap.tonight, R.mipmap.tonight_r);
                clearCheck(button7, R.mipmap.stanight, R.mipmap.stanight_r);
                break;
            case R.id.button4:

                setcheck(button4, R.mipmap.tonight, R.mipmap.tonight_r_r);
                msg = button4.isCheck ? GetSystemTimeUtil.getInDetailTime(GetSystemTimeUtil.getPresentSysNighttime()) : null;
                button1.setName(msg == null ? GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()) : GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailTime(GetSystemTimeUtil.getPresentSysNighttime())));
                postion = button4.isCheck ? 4 : -1;
                timeDataCallBack.getSelectTime(msg);
                clearCheck(button6, R.mipmap.frinight, R.mipmap.frinight_r);
                clearCheck(button5, R.mipmap.tomorrownight, R.mipmap.tomorrownight);
                clearCheck(button7, R.mipmap.stanight, R.mipmap.stanight_r);
                clearCheck(button3, R.mipmap.todaynoon, R.mipmap.todaynoon_r);
                break;
            case R.id.button5:

                setcheck(button5, R.mipmap.tomorrownight, R.mipmap.tomorrownight_r_r);
                msg = button5.isCheck ? GetSystemTimeUtil.getTomoreaoyDate() : null;
                button1.setName(msg == null ? GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()) : GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getTomoreaoyDate()));
                postion = button5.isCheck ? 5 : -1;
                timeDataCallBack.getSelectTime(msg);
                clearCheck(button6, R.mipmap.frinight, R.mipmap.frinight_r);
                clearCheck(button7, R.mipmap.stanight, R.mipmap.stanight_r);
                clearCheck(button4, R.mipmap.tonight, R.mipmap.tonight_r);
                clearCheck(button3, R.mipmap.todaynoon, R.mipmap.todaynoon_r);
                // msg=button5.getText().toString();
                break;
            case R.id.button6:

                setcheck(button6, R.mipmap.frinight, R.mipmap.frinight_r_r);
                msg = button6.isCheck ? GetSystemTimeUtil.getFridayNightDate() : null;
                button1.setName(msg == null ? GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()) : GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getFridayNightDate()));
                postion = button6.isCheck ? 6 : -1;
                timeDataCallBack.getSelectTime(msg);
                clearCheck(button7, R.mipmap.stanight, R.mipmap.stanight_r);
                clearCheck(button5, R.mipmap.tomorrownight, R.mipmap.tomorrownight);
                clearCheck(button4, R.mipmap.tonight, R.mipmap.tonight_r);
                clearCheck(button3, R.mipmap.todaynoon, R.mipmap.todaynoon_r);
                // msg=button6.getText().toString();
                break;
            case R.id.button7:

                setcheck(button7, R.mipmap.stanight, R.mipmap.stanight_r_r);
                msg = button7.isCheck ? GetSystemTimeUtil.getStatNightDate() : null;
                button1.setName(msg == null ? GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()) : GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getStatNightDate()));
                postion = button3.isCheck ? 7 : -1;
                timeDataCallBack.getSelectTime(msg);
                clearCheck(button6, R.mipmap.frinight, R.mipmap.frinight_r);
                clearCheck(button5, R.mipmap.tomorrownight, R.mipmap.tomorrownight);
                clearCheck(button4, R.mipmap.tonight, R.mipmap.tonight_r);
                clearCheck(button3, R.mipmap.todaynoon, R.mipmap.todaynoon_r);
                //  msg=button7.getText().toString();
                break;


        }
        SharedPref.getInstance(App.getInstance()).putInt(Constant.TIME_SELECT_KEY,postion);

    }

    public void setButtonName(String msg) {
        button1.setName(msg == null ? GetSystemTimeUtil.getNowTurnTime(GetSystemTimeUtil.getInDetailgetTime()) : GetSystemTimeUtil.getNowTurnTime(msg));
    }

    public void setSelectStatus() {
        int postion = SharedPref.getInstance(App.getInstance()).getInt(Constant.TIME_SELECT_KEY,-1);

        if (postion == 3) {
            button3.setCheckDrawable(R.mipmap.todaynoon_r_r);
            button3.setSingleTextColor(getResources().getColor(R.color.time_select_color) );
        } else if (postion == 4) {
            button4.setCheckDrawable(R.mipmap.tonight_r_r);
            button4.setSingleTextColor(getResources().getColor(R.color.time_select_color) );
        } else if (postion == 5) {
            button5.setCheckDrawable(R.mipmap.tomorrownight_r_r);
            button5.setSingleTextColor(getResources().getColor(R.color.time_select_color) );
        } else if (postion == 6) {
            button6.setCheckDrawable(R.mipmap.frinight_r_r);
            button6.setSingleTextColor(getResources().getColor(R.color.time_select_color) );
        } else if (postion == 7) {
            button7.setCheckDrawable(R.mipmap.stanight_r_r);
            button7.setSingleTextColor(getResources().getColor(R.color.time_select_color) );
        }


    }

    public void finshSeldct() {
        SharedPref.getInstance(App.getInstance()).putInt(Constant.TIME_SELECT_KEY,-1);

    }

    public void clearCheck(TimeButton timeButton, int uncheckid, int unclick) {
        timeButton.setCheck(false);
        timeButton.setCheckDrawable(timeButton.isClickable() ? uncheckid : unclick);
    }


    public void setcheck(TimeButton timeButton, int checkid, int uncheckid) {
        if (timeButton.isCheck) {
            timeButton.setCheck(false);
            timeButton.setCheckDrawable(checkid);
        } else {
            timeButton.setCheck(true);
            timeButton.setCheckDrawable(uncheckid);
        }
    }

    public interface TimeDataCallBack {
        void getSelectTime(String time);
    }
}
